package com.ddmh.minio.factory;

import com.ddmh.minio.config.MinioProperties;
import io.minio.MinioClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * Minio客户端工厂
 *
 * @author brant
 * @since 2024-04-12
 */
@Slf4j
@Component
public class MinioClientFactory {
    /**
     * Minio客户端
     */
    private static MinioClient minioClient;
    /**
     * Minio配置
     */
    @Autowired
    private MinioProperties minioProperties;

    /**
     * 获取Minio客户端
     *
     * @return 客户端实例
     */
    public static MinioClient getInstance() {
        return minioClient;
    }

    /**
     * 初始化
     */
    @PostConstruct
    private void init() {
        log.info("minio客户端初始化...");
        minioClient = MinioClient.builder()
            .endpoint(minioProperties.getEndpoint())
            .credentials(minioProperties.getAccessKey(), minioProperties.getSecretKey())
            .build();
        minioClient.setTimeout(60 * 60 * 1000, 60 * 60 * 1000, 60 * 60 * 1000);
        log.info("minio客户端初始化完成");
    }

}
